#include <QtWidgets>

#include "mprocess.h"
#include "marrow.h"
#include "mtext.h"
#include <QHashIterator>

mProcess::mProcess(int id, QGraphicsItem *parent, QGraphicsScene *scene ):QGraphicsPolygonItem(parent)

{
    int ItemWidth = 40;
    int ItemHeight = 24;
    int radis = 99; //0-99 后面两个参数的范围0-99，值越大越圆
    myID=id;
     myPolygon<< QPointF(-ItemWidth, -ItemHeight) << QPointF(ItemWidth, -ItemHeight)
        << QPointF(ItemWidth, ItemHeight) << QPointF(-ItemWidth, ItemHeight)
        << QPointF(-ItemWidth, -ItemHeight);
    QPainterPath path;
    //QRectF rect = QRectF(0,0,48,48);  //两点确定矩形范围，一般为控件大小，这样可以切割四个圆角，也可以调整大小，调整圆角个数
    path.addRoundRect(myPolygon.boundingRect(),radis,radis);   //后面两个参数的范围0-99，值越大越园
    myPolygon= path.toFillPolygon().toPolygon();//获得这个路径上的所有的点
    setPolygon(myPolygon);
    setFlag(QGraphicsItem::ItemIsMovable, true);
    setFlag(QGraphicsItem::ItemIsSelectable, true);
    setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
}
void mProcess::addArrow(mArrow *arrow)
{
    arrows.append(arrow);
}

void mProcess::removeArrow(mArrow *arrow)
{
    int index = arrows.indexOf(arrow);

    if (index != -1)
    {
        arrows.removeAt(index);
    }
}

QList<int> mProcess::removeArrows()
{
    QList<int> lst_ArrowID;
    foreach (mArrow *arrow, arrows) {
        arrow->startItem()->removeArrow(arrow);
        arrow->endItem()->removeArrow(arrow);
        scene()->removeItem(arrow);
        lst_ArrowID.append(arrow->GetMyID());
        delete arrow;
    }
    return lst_ArrowID;
}

void mProcess::addText(mText *text)
{
    texts.append(text);
}

void mProcess::removeText(mText *text)
{
    int index = texts.indexOf(text);

    if (index != -1)
        texts.removeAt(index);
}

void mProcess::removeTexts()
{
    foreach (mText *text, texts) {
        text->BindTextItem()->removeText(text);
        scene()->removeItem(text);
        delete text;
    }
}

QVariant mProcess::itemChange(GraphicsItemChange change,
                     const QVariant &value)
{
    if (change == QGraphicsItem::ItemPositionChange) {
        foreach (mArrow *arrow, arrows) {
            arrow->updatePosition();
        }
        foreach (mText *text, texts) {
            text->updatePosition();
        }
    }

    return value;
}

void mProcess::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QStyleOptionGraphicsItem op;
    op.initFrom(widget);
    op.state = QStyle::State_None;
    QGraphicsPolygonItem::paint(painter, &op, widget);
}
 void mProcess::addInfo(CState *stateInfo){

     StateInfo=stateInfo;
 }

CState* mProcess::getStateInfo(){
        return this->StateInfo;
    }
